package net.hn.hnms.biz.plan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.hn.hnms.biz.plan.domain.ExecutionFootageOutput;
import net.hn.hnms.biz.plan.request.ExecutionFootageOutputReq;
import net.hn.hnms.biz.plan.vo.ExecutionFootageOutputVO;
import net.hn.hnms.biz.plan.vo.MiningFaceScheduleVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ExecutionFootageOutputMapper extends BaseMapper<ExecutionFootageOutput> {

    @Select("<script> select COALESCE(sum(footage),0) as footage,COALESCE(sum(output),0) as output from execution_footage_output where del_flag='0' and work_face_id = #{id} </script>")
    ExecutionFootageOutput selectFootageAndDevelop (@Param("id") String id);


    @Select("<script> " +
            " \n" +
            " SELECT * \n" +
            " FROM execution_footage_output \n" +
            " WHERE  " +
            "  execution_date = TO_DATE(#{weekTimeStart}, 'YYYY-MM-DD') " +
            "            <if test='id!= null and id != \"\"'>" +
            "                AND  work_face_id = #{id} \n" +
            "            </if>" +
            " ORDER BY \n" +
            "    CASE shift_id\n" +
            "        WHEN '0298c8a9d0639d715a0925f7b1ec2f55' THEN 1\n" +
            "        WHEN '0e0220e4af825d70d0932cc4504cd80d' THEN 2\n" +
            "        WHEN '70b1dacd7bf4db32cd37c1322a290005' THEN 3\n" +
            "    END; " +
            "  </script>")
    List<ExecutionFootageOutput> detailFootageAndDevelopByData (@Param("id") String id,@Param("weekTimeStart") String weekTimeStart);

    @Select("<script> select execution_date,footage,output from  ( " +
            "select " +
            " t1.day as execution_date, " +
            " sum(case  when  footage is  null then 0 else footage end) as footage, " +
            " sum(case  when  output is  null then 0 else output end) as output " +
            " from ( " +
            " SELECT DATE( T ) AS day " +
            " FROM " +
            " generate_series ( ( CURRENT_TIMESTAMP - INTERVAL '7 day' ) :: DATE,CURRENT_TIMESTAMP :: DATE, '1 days' ) AS T ) t1 " +
            " left join  " +
            " (select execution_date,sum(footage) as footage,sum (output) as output  from execution_footage_output  " +
            "  where  work_face_id = #{id}  and del_flag = '0' " +
            " GROUP  by execution_date ) t2 on t1.day = t2.execution_date " +
            " group by t1.day order by t1.day  ) as o  where   execution_date <![CDATA[ < ]]> current_date " +
            " </script>")
    List<ExecutionFootageOutput> selectLastSevenDaysDetailFD (@Param("id") String id);


    @Select("<script>" +
            " select  " +
            " sum(footage) as totalFootage," +
            " sum(output) as totalOutput," +
            " sum(knife_quantity) as totalKnifeQuantity " +
            " from execution_footage_output " +
            " where del_flag ='0' and  mine_code =  #{mineCode} " +
            "</script>")
    ExecutionFootageOutputVO selectPlanOutPutTotalByMineCode(@Param("mineCode") String mineCode);

    @Select("<script>" +
            " select  " +
            " sum(footage) as totalFootage," +
            " sum(output) as totalOutput," +
            " sum(knife_quantity) as totalKnifeQuantity " +
            " from execution_footage_output " +
            " where del_flag ='0' and  company_code =  #{mineCode} " +
            "</script>")
    ExecutionFootageOutputVO selectDimDrivageFaceTotalByCompanyCodeOrName(@Param("mineCode") String mineCode);

    @Select("<script>" +
            " select  " +
            " sum(footage) as footage from " +
            " execution_footage_output where execution_date     <![CDATA[ >=  ]]> to_date( #{weekTimeStart}, 'YYYY-MM-DD' )       and execution_date   <![CDATA[ <=  ]]> to_date( #{weekTimeEnd}, 'YYYY-MM-DD' )  " +
            "            <if test='faceId!= null and faceId != \"\"'>" +
            "                AND  work_face_id = #{faceId} \n" +
            "            </if>" +
            " and del_flag ='0' " +
            " and work_face_type = '1' " +
            "</script>")
    ExecutionFootageOutputVO selectPlanOutPutByFaceIdAndDate(@Param("faceId") String faceId,@Param("weekTimeStart") String weekTimeStart,@Param("weekTimeEnd") String weekTimeEnd);

    @Select("<script>" +
            " select  " +
            " sum(footage) as footage from " +
            " execution_footage_output where  work_face_id =#{faceId}  and execution_date <![CDATA[ <=  ]]> to_date( #{weekTimeStart}, 'YYYY-MM-DD' )" +
            " and del_flag ='0' " +
            "</script>")
    ExecutionFootageOutputVO selectPlanOutPutByFaceId(@Param("faceId") String faceId,@Param("weekTimeStart") String weekTimeStart,@Param("weekTimeEnd") String weekTimeEnd);

    @Select("SELECT count(DISTINCT execution_date ) FROM execution_footage_output where work_face_id = #{id} and del_flag ='0'  ")
    int selectDay(@Param("id") String weekId );

    ExecutionFootageOutputVO getYearOrMonthFootageOutput(@Param("req") ExecutionFootageOutputReq req);


    MiningFaceScheduleVo getMiningFaceSchedule(@Param("id") String id);
}
